/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package list;

import entity.Pengguna;
import java.util.AbstractList;
import java.util.List;
import jpa.PenggunaJpaController;

/**
 *
 * @author radityo
 */
public class PenggunaList extends AbstractList<Pengguna> {

    private int pageSize;

    private int page;

    private List<Pengguna> list;

    private PenggunaJpaController jpa;

    public PenggunaList(int size){
        super();
        this.pageSize = size;
        page = 1;
        jpa = new PenggunaJpaController();
        list = jpa.findPenggunaEntities(size, 0);

    }

    

    @Override
    public int size() {
        return jpa.getPenggunaCount();
    }


    private void calculate(int index){
        int page = -1;
        
        if(index+1 <= pageSize){
            page = 1;
        }else{
            page = (int)(index/pageSize);
            if(index%pageSize != 0){
                page++;
            }
        }

        if(this.page != page){
            list = jpa.findPenggunaEntities(pageSize, index);
            this.page = page;
        }

    }

    @Override
    public Pengguna get(int index) {
        
        calculate(index);

        

        if(index < pageSize){

            if(index > list.size()){
                return new Pengguna();
            }
            
            return list.get(index);
        }else{

            if(index%pageSize > list.size()){
                return new Pengguna();
            }
            
            return list.get(index%pageSize);
        }
         
        //return new Pengguna();
    }

}
